package com.dragon.logical.service.wechat.impl;

import com.alibaba.fastjson.serializer.MapSerializer;
import com.dragon.common.util.RedisUtil;
import com.dragon.common.util.WechatCheckUtil;
import com.dragon.logical.service.auth.IOperationAutoService;
import com.dragon.logical.service.wechat.WechatUtilService;
import com.dragon.repository.entity.OperationAuto;
import com.dragon.repository.entity.WechatArticle;
import com.dragon.repository.entity.WechatMenu;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class WechatUtilServiceImpl implements WechatUtilService {


    @Autowired
    RedisUtil redisUtil;

    @Autowired
    IOperationAutoService operationAutoService;


    //redis 缓存前缀
    private final static String ACCESS_TOKEN_KEY = "WECHAT_ACCESS_KEY";

    private final static String APP_ID_NAME = "app_id";

    private final static String SECRET_NAME = "secret";


    @Override
    public String getAccessToken(){
        Object accessToken = redisUtil.get(ACCESS_TOKEN_KEY);
        if(accessToken !=null){
            return accessToken.toString();
        }
        // 获取appid，以及密匙
        OperationAuto operationAuto1 = operationAutoService.selectDetailBySystemCode(APP_ID_NAME);
        OperationAuto operationAuto2 = operationAutoService.selectDetailBySystemCode(SECRET_NAME);
        return WechatCheckUtil.getAccessToken(operationAuto1.getValue(),operationAuto2.getValue());
    }


    @Override
    public String addDart(WechatArticle wechatArticle){
        Map<String,String> map = new HashMap<>();
        map.put("title", wechatArticle.getTitle());
        map.put("content", wechatArticle.getContent());
        map.put("author",wechatArticle.getAuthor());
        map.put("digest",wechatArticle.getDigest());
        map.put("content_source_url",wechatArticle.getContentSourceUrl());
        map.put("thumb_media_id",wechatArticle.getThumbMediaId());
        map.put("need_open_comment", String.valueOf(wechatArticle.getNeedOpenComment()));
        map.put("only_fans_can_comment", String.valueOf(wechatArticle.getOnlyFansCanComment()));
        return WechatCheckUtil.addDart(getAccessToken(),map);
    }


    @Override
    public Boolean updateDart(WechatArticle wechatArticle){
        Map<String,Object> objectMap = new HashMap<>();
        objectMap.put("media_id",wechatArticle.getMediaId());
        objectMap.put("index",wechatArticle.getSort());
        Map<String,String> map = new HashMap<>();
        map.put("title", wechatArticle.getTitle());
        map.put("content", wechatArticle.getContent());
        map.put("author",wechatArticle.getAuthor());
        map.put("digest",wechatArticle.getDigest());
        map.put("content_source_url",wechatArticle.getContentSourceUrl());
        map.put("thumb_media_id",wechatArticle.getThumbMediaId());
        map.put("need_open_comment", String.valueOf(wechatArticle.getNeedOpenComment()));
        map.put("only_fans_can_comment", String.valueOf(wechatArticle.getOnlyFansCanComment()));
        objectMap.put("articles",map);
        Boolean result = WechatCheckUtil.updateDart(getAccessToken(),objectMap);
        return result;
    }

    @Override
    public Boolean deleteDart(String mediaId){
        return WechatCheckUtil.deleteDart(getAccessToken(),mediaId);
    }


    @Override
    public String releaseArticle(String mediaId){
        return WechatCheckUtil.releaseArticle(getAccessToken(),mediaId);
    }

    @Override
    public String getArticleStatus(String publishId){
        return WechatCheckUtil.getArticleStatus(getAccessToken(),publishId);
    }

    @Override
    public Map<String,String> uploadFile(String path,String type){
        return WechatCheckUtil.uploadFile(getAccessToken(),path,type);
    }

    @Override
    public Boolean addWechatMenu(List<WechatMenu> object){
        return WechatCheckUtil.addMenu(getAccessToken(),object);
    }

    @Override
    public Boolean deleteWechatMenu() {
        return WechatCheckUtil.deleteMenu(getAccessToken());
    }
}
